package com.example.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.admin.service.BlogService;
import com.example.xo.common.pojo.Blog;
import com.example.xo.common.result.Result;
import com.example.xo.vo.BlogVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

/**
 * @author liguoping
 */
@Slf4j
@RestController
@RequestMapping("blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @PostMapping("add")
    public Result add(@RequestBody Blog blog) {
            log.info("{}",blog);
        if (blog==null||blog.getTitle()==null){
            return Result.error();
        }
        //判断blog不为空
        if (ObjectUtil.isNull(blog)) {
            return Result.error();
        }
        System.out.println(blog);
        //标题不为空不超过50个字
        if (StrUtil.isEmpty(blog.getTitle().trim()) || blog.getTitle().length() > 50) {
            //日志，记录给程序员
            log.info("标题不能为空{}字符个数不能超过50", blog.getTitle());
            System.out.println(blog.getTitle());
            return Result.error();
        }
        //作者不能为空，长度不能超过10
        if (StrUtil.isEmpty(blog.getAuthor().trim()) || blog.getAuthor().length() > 10) {
            log.info("作者不为空，长度不超过10");
            return Result.error();
        }
        //内容不能为空
        if (StrUtil.isEmpty(blog.getContent().trim())) {
            log.info("内容不为空，长度不限制");
            return Result.error();
        }
        boolean res = blogService.add(blog);
        if (!res){
            log.info("{}",res);
            return Result.error();
        }
        //返回添加成功
        return Result.success(blogService.add(blog));
//不能为空 trim 去除两端空格，中间的空格去不掉
//        if (blog.getTitle() == null || blog.getTitle().trim().length() == 0) {
//            log.info("标题为空");
//            return Result.error();
//        }
//        //标题不超过50个字
//        if (blog.getTitle().length() > 50) {
//            log.info("标题过长");
//            return Result.error();
//        }
//        //判断分类
//        if (blog.getCategoryId() == null || blog.getCategoryId().trim().length() == 0) {
//            log.info("分类数据丢失");
//            return Result.error();
//        }
//
//        if (blog.getAuthor() == null || blog.getAuthor().trim().length() == 0) {
//            log.info("作者数据丢失");
//            return Result.error();
//        }
//        if (blog.getAuthor().length() > 10) {
//            log.info("作者名字过长");
//            return Result.error();
//        }
//        return Result.success(blogService.add(blog));


    }

    @PutMapping("update")
    public Result update(@RequestBody Blog blog) {
        log.info("{}",blog);
        boolean update = blogService.update(blog);
        if (update){
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("findAll")
    public Result findAll() {
        return Result.success(blogService.findAll());
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable String id) {
        return Result.success(blogService.findById(id));
    }
    @GetMapping("findById/{id}")
    public Result findBlogId(@PathVariable String id) {
        return Result.success(blogService.findPoById(id));
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(blogService.deleteById(id));
    }
}
